home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / sndplaydoublebuffer / _source / doublebufferfromfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  30.4 KB  |  1,104 lines

  1. /*
  2.     File:        DoubleBufferFromFile.c
  3.  
  4.     Contains:    Routines demonstrating how to play a sound from disk using SndPlayDoubleBuffer.
  5.  
  6.     Written by: Mark Cookson    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/31/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "MyAIFF.h"
  25. #include "DoubleBufferFromFile.h"
  26.  
  27. /*    Purpose:        This creates a new SoundInfo structure and initializes
  28.                     it by calling the private function ASoundInit.
  29.     Side Effects:    None.
  30. */
  31. /*-----------------------------------------------------------------------*/
  32. SoundInfoPtr    ASoundNew                (OSErr *theErr)
  33. /*-----------------------------------------------------------------------*/
  34. {
  35.     SoundInfoPtr    theSoundInfo    = nil;
  36.  
  37.     *theErr = noErr;
  38.     theSoundInfo = (SoundInfoPtr)NewPtrClear (sizeof (SoundInfo));
  39.  
  40.     if (MemError () != noErr || theSoundInfo == nil) {
  41.         *theErr = kInitErr;
  42.     }
  43.     else {
  44.         *theErr = ASoundInit (theSoundInfo);
  45.     }
  46.  
  47.     if (*theErr != noErr) {
  48.         DebugPrint ("\pError in ASoundNew");
  49.     }
  50.  
  51.     return theSoundInfo;
  52. }
  53.  
  54. /*
  55.     Purpose:        Display a StandardFile dialog to select a file.
  56.                     Opens the file selected by the user.
  57.     Side Effects:    None.
  58. */
  59. /*-----------------------------------------------------------------------*/
  60. OSErr            ASoundGetFileToPlay        (SoundInfoPtr theSoundInfo)
  61. /*-----------------------------------------------------------------------*/
  62. {
  63.     FileFilterUPP        myFilterUPP    = nil;
  64.     OSErr                theErr        = noErr;
  65.     short                i            = 0;
  66.     Boolean                good        = false;
  67.  
  68.     if (IsValid (theSoundInfo)) {
  69.         myFilterUPP = NewFileFilterProc (ASoundFileFilter);
  70.         if ((theSoundInfo->globals.ggestaltStandardFileAttr & gestaltStandardFile58) == false) {
  71.             StandardFileReply    theSFReply;
  72.             short                showTypes;
  73.  
  74.             if ((theSoundInfo->globals.ggestaltTranslationAttr & (1 << gestaltTranslationMgrExists)) == true) {
  75.                 showTypes = kUseOpenResourceTypes;
  76.             }
  77.             else {
  78.                 showTypes = kNoFirstFiltering;
  79.             }
  80.             StandardGetFile (myFilterUPP, showTypes, nil, &theSFReply);
  81.             theSoundInfo->vRefNum = theSFReply.sfFile.vRefNum;
  82.             if (theSFReply.sfGood == true) {
  83.                 switch (theSFReply.sfType) {
  84.                     case kSNDResource:
  85.                     case kResource:
  86.                         theErr = FSpOpenRF (&theSFReply.sfFile, fsCurPerm, &(theSoundInfo->refNum));
  87.                         break;
  88.                     case kCompressedAIFFFile:
  89.                     case kUncompressedAIFFFile:
  90.                     case kWAVEFile:
  91.                     case kWAVFile:
  92.                     case kAUFile:
  93.                         theErr = FSpOpenDF (&theSFReply.sfFile, fsCurPerm, &(theSoundInfo->refNum));
  94.                         break;
  95.                     default:
  96.                         DebugPrint ("\pASoundGetFileToPlay should have never executed this line");
  97.                 }
  98.                 if (theErr != noErr) {
  99.                     DebugPrint ("\pCouldn't open file");
  100.                 }
  101.                 else {
  102.                     theSoundInfo->fileType = theSFReply.sfType;
  103.                     for (i = 0; i <= theSFReply.sfFile.name[0]+1; i++)
  104.                         theSoundInfo->theName[i] = theSFReply.sfFile.name[i];
  105.                 }
  106.             }
  107.             else {
  108.                 theErr = userCanceledErr;
  109.             }
  110.         }
  111.         else {
  112.             SFReply            theSFReply;
  113.             Point            where        = {kInit, kInit};
  114.             Rect            screenRect    = GetMainScreenRect();
  115.             long            dirID        = kInit,
  116.                             procID        = kInit;
  117.  
  118.             where.h = screenRect.right / kHorizAdjust;    /* This will put the SFGetFile dialog in the same place */
  119.             where.v = screenRect.bottom / kVertAdjust;    /* as the StandardGetFile dialog (or really close). */
  120.             if ((GetMBarHeight() + kOne) > where.h) {
  121.                 where.h = GetMBarHeight() + kOne;
  122.             }
  123.             SFGetFile (where, nil, myFilterUPP, kNoFirstFiltering, nil, nil, &theSFReply);
  124.             good = theSFReply.good;
  125.             if (theSFReply.good == true) {
  126.                 theErr = GetWDInfo (theSFReply.vRefNum, &(theSoundInfo->vRefNum), &dirID, &procID);
  127.                 if (theErr == noErr) {
  128.                     switch (theSFReply.fType) {
  129.                         case kSNDResource:
  130.                         case kResource:
  131.                             theErr = HOpenRF (theSoundInfo->vRefNum, dirID, theSFReply.fName, fsCurPerm, &(theSoundInfo->refNum));
  132.                             break;
  133.                         case kCompressedAIFFFile:
  134.                         case kUncompressedAIFFFile:
  135.                         case kWAVEFile:
  136.                         case kWAVFile:
  137.                         case kAUFile:
  138.                             theErr = HOpenDF (theSoundInfo->vRefNum, dirID, theSFReply.fName, fsCurPerm, &(theSoundInfo->refNum));
  139.                             break;
  140.                         default:
  141.                             DebugPrint ("\pASoundGetFileToPlay should have never executed this line");
  142.                     }
  143.                     if (theErr != noErr) {
  144.                         DebugPrint ("\pCouldn't open file");
  145.                     }
  146.                     else {
  147.                         theSoundInfo->fileType = theSFReply.fType;
  148.                         for (i = 0; i <= theSFReply.fName[0]+1; i++)
  149.                             theSoundInfo->theName[i] = theSFReply.fName[i];
  150.                     }
  151.                 }
  152.                 else {
  153.                     theErr = kFileErr;
  154.                     DebugPrint ("\pCouldn't translate working directory");
  155.                 }
  156.             }
  157.             else {
  158.                 theErr = userCanceledErr;
  159.             }
  160.         }
  161.         DisposeRoutineDescriptor (myFilterUPP);
  162.     }
  163.     else {
  164.         theErr = kNilPtrErr;
  165.     }
  166.  
  167.     if (theErr != noErr && theErr != userCanceledErr) {
  168.         DebugPrint ("\pError in ASoundGetFileToPlay");
  169.     }
  170.  
  171.     return theErr;
  172. }
  173.  
  174. /*
  175.     Purpose:        Checks a file to see if its header can be parsed
  176.                     and the file can be played.
  177.  
  178.                     This will return an error if the sound will not play,
  179.                     returning noErr means that sound will play.
  180.  
  181.                     Parsing a header can take some time, this routine
  182.                     is a canidate for speed improvements.
  183.     Side Effects:    None.
  184. */
  185. /*-----------------------------------------------------------------------*/
  186. OSErr            ASoundCanThisPlay        (CInfoPBPtr theFileInfo)
  187. /*-----------------------------------------------------------------------*/
  188. {
  189.     SoundInfoPtr    theSoundInfo    = nil;
  190.     long            dataStart        = kInit,
  191.                     sndLength        = kInit;
  192.     OSErr            theErr            = noErr,
  193.                     closeErr        = noErr;
  194.  
  195.     if (theFileInfo  != nil) {
  196.         theSoundInfo = ASoundNew (&theErr);
  197.         if (theErr == noErr) {
  198.             theSoundInfo->vRefNum = theFileInfo->hFileInfo.ioVRefNum;
  199.             theSoundInfo->fileType = theFileInfo->hFileInfo.ioFlFndrInfo.fdType;
  200.             theErr = HOpenDF (theFileInfo->hFileInfo.ioVRefNum, theFileInfo->hFileInfo.ioFlParID, theFileInfo->hFileInfo.ioNamePtr, fsRdPerm, &theSoundInfo->refNum);
  201.             if (theErr == noErr) {
  202.                 switch (theSoundInfo->fileType) {
  203.                     case kCompressedAIFFFile:
  204.                     case kUncompressedAIFFFile:
  205.                         theErr = ASoundGetAIFFHeader (theSoundInfo, &dataStart, &sndLength);
  206.                         break;
  207.                     case kWAVEFile:
  208.                     case kWAVFile:
  209.                         theErr = ASoundGetWAVEHeader (theSoundInfo, &dataStart, &sndLength);
  210.                         break;
  211.                     case kAUFile:
  212.                         theErr = ASoundGetULAWHeader (theSoundInfo, &dataStart, &sndLength);
  213.                         break;
  214.                     case kSNDResource:
  215.                     case kResource:
  216.                         theErr = noErr;
  217.                         break;
  218.                     default:
  219.                         /* The file type is not one that we can even parse. */
  220.                         theErr = kUnknownFormat;
  221.                         break;
  222.                 }
  223.             }
  224.         }
  225.     }
  226.     else {
  227.         theErr = kNilPtrErr;
  228.     }
  229.  
  230.     closeErr = FSClose (theSoundInfo->refNum);
  231.     DisposeRoutineDescriptor (ASoundGetSoundCallBack (theSoundInfo));
  232.     DisposePtr ((Ptr)theSoundInfo);
  233.  
  234.     if (closeErr != noErr) {
  235.         theErr = closeErr;
  236.     }
  237.  
  238.     if (theErr != noErr && theErr != kUnknownFormat) {
  239.         DebugPrint ("\pError in ASoundCanThisPlay");
  240.     }
  241.  
  242.     return theErr;
  243. }
  244.  
  245. /*
  246.     Purpose:        This function is called to get ready to play a sound.
  247.                     Use this if you want to make sure that there is enough
  248.                     memory to play the sound.
  249.     Side Effects:    This will call routines that will allocate memory needed
  250.                     to all of the various structures needed by the Sound Manager
  251.                     and memory to be used as the sounds' buffers.
  252. */
  253. /*-----------------------------------------------------------------------*/
  254. OSErr            ASoundReadyForPlaying    (SoundInfoPtr theSoundInfo,
  255.                                         unsigned long bufferSize)
  256. /*-----------------------------------------------------------------------*/
  257. {
  258.     OSErr                theErr        = noErr;
  259.  
  260.     if (IsValid (theSoundInfo)) {
  261.         if (theSoundInfo->globals.gSupportsSPDB == false) {
  262.             DebugPrint ("\pThis machine doesn't support SoundPlayDoubleBuffer, can't play!");
  263.             theErr = kNoSPDBErr;
  264.         }
  265.         else {
  266.             /* do we want to start at the begining of the file? */
  267.             if ((ASoundGetBytesCopied (theSoundInfo) <= kMaxAIFFHeaderSize) && theSoundInfo->paused == false) {
  268.                 theSoundInfo->soundDone = false;
  269.                 theErr = SndNewChannel (&(theSoundInfo->chan), sampledSynth, nil, ASoundGetSoundCallBack (theSoundInfo));
  270.                 if (theErr != noErr) {
  271.                     DebugPrint ("\pSndNewChannel error!");
  272.                 }
  273.                 else {
  274.                     theErr = SetUpSoundHeader (theSoundInfo, bufferSize);
  275.                     if (theErr == noErr) {
  276.                         theErr = ASoundPrimeBuffers (theSoundInfo);
  277.                         ASoundSetCurBuffer (theSoundInfo, kStart);
  278.                     }
  279.                 }
  280.             }
  281.             else {
  282.                 theErr = ASoundPrimeBuffers (theSoundInfo);
  283.             }
  284.         }
  285.     }
  286.     else {
  287.         theErr = kNilPtrErr;
  288.     }
  289.  
  290.     if (theErr != noErr) {
  291.         DebugPrint ("\pError in ASoundReadyForPlaying!");
  292.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  293.     }
  294.  
  295.     return theErr;
  296. }
  297.  
  298. /*
  299.     Purpose:        Call this after you have called ASoundReadyForPlaying to
  300.                     start playing the sound you prepaired.
  301.     Side Effects:    Starts the sound playing.
  302. */
  303. /*-----------------------------------------------------------------------*/
  304. OSErr            ASoundPlay                (SoundInfoPtr theSoundInfo)
  305. /*-----------------------------------------------------------------------*/
  306. {
  307.     OSErr                theErr        = noErr;
  308.  
  309.     if (IsValid (theSoundInfo)) {
  310.         if (theSoundInfo->playing == false || theSoundInfo->paused == true) {
  311.             if (theSoundInfo->globals.gSupportsSPDB == false) {
  312.                 theErr = kNoSPDBErr;
  313.                 DebugPrint ("\pThis machine doesn't support SoundPlayDoubleBuffer, can't play!");
  314.             }
  315.             else {
  316.                 if (theSoundInfo->paused == false) {
  317.                     theErr = SndPlayDoubleBuffer (theSoundInfo->chan, (SndDoubleBufferHeaderPtr)&(theSoundInfo->doubleHeader));
  318.                     if (theErr == noErr) {
  319.                         theErr = InstallCallBack (theSoundInfo);
  320.                     }
  321.                     if (theErr == noErr) {
  322.                         theSoundInfo->playing = true;
  323.                     }
  324.                 }
  325.             }
  326.         }
  327.     }
  328.     else {
  329.         theErr = kNilPtrErr;
  330.     }
  331.  
  332.     if (theErr != noErr) {
  333.         DebugPrint ("\pError in ASoundPlay!");
  334.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  335.     }
  336.  
  337.     return theErr;
  338. }
  339.  
  340. /*
  341.     Purpose:        Wrapper function called to start playing a sound.
  342.                     Use this if you are pretty sure the sound will play, or
  343.                     just don't care specifically what goes wrong.
  344.     Side Effects:    This will call routines that will allocate memory needed
  345.                     for all of the various structures needed by the Sound Manager
  346.                     and memory to be used as the sounds' buffers.
  347. */
  348. /*-----------------------------------------------------------------------*/
  349. OSErr            ASoundStartPlaying        (SoundInfoPtr theSoundInfo,
  350.                                         unsigned long bufferSize)
  351. /*-----------------------------------------------------------------------*/
  352. {
  353.     OSErr                theErr        = noErr;
  354.  
  355.     theErr = ASoundReadyForPlaying (theSoundInfo, bufferSize);
  356.     if (theErr == noErr) {
  357.         theErr = ASoundPlay (theSoundInfo);
  358.     }
  359.  
  360.     if (theErr != noErr) {
  361.         DebugPrint ("\pError in ASoundStartPlaying!");
  362.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  363.     }
  364.  
  365.     return theErr;
  366. }
  367.  
  368. /*
  369.     Purpose:        Stops the currently playing sound.
  370.     Side Effects:    Stopping the currently playing sound will cause the
  371.                     sound completion routine to run.
  372. */
  373. /*-----------------------------------------------------------------------*/
  374. OSErr            ASoundStop                (SoundInfoPtr theSoundInfo)
  375. /*-----------------------------------------------------------------------*/
  376. {
  377.     SndCommand        theCmd        = {quietCmd, kInit, kInit};
  378.     OSErr            theErr        = noErr;
  379.  
  380.     if (StrictIsValid (theSoundInfo)) {
  381.         /* So that ASoundDoubleBackProc knows that it doesn't have to
  382.            actually read any data */
  383.         theSoundInfo->stopping = true;
  384.         theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  385.         if (theErr == noErr) {
  386.             theSoundInfo->playing = false;
  387.             theSoundInfo->paused = false;
  388.         }
  389.     }
  390.     else {
  391.         theErr = kNilPtrErr;
  392.     }
  393.  
  394.     if (theErr != noErr) {
  395.         DebugPrint ("\pError in ASoundStop");
  396.     }
  397.  
  398.     return theErr;
  399. }
  400.  
  401. /*
  402.     Purpose:        Wrapper so the user doesn't have to keep track of if
  403.                     the sound is playing or not.
  404.     Side Effects:    If resuming a sound and the user had also called
  405.                     ASoundPauseForAdjust this will reinstall the sound
  406.                     completion callback.
  407. */
  408. /*-----------------------------------------------------------------------*/
  409. OSErr            ASoundPause                (SoundInfoPtr theSoundInfo)
  410. /*-----------------------------------------------------------------------*/
  411. {
  412.     OSErr            theErr        = noErr;
  413.  
  414.     if (StrictIsValid (theSoundInfo)) {
  415.         if (theSoundInfo->paused == false) {
  416.             theErr = PauseSound (theSoundInfo);
  417.         }
  418.         else {
  419.             theErr = ResumeSound (theSoundInfo);
  420.         }
  421.     }
  422.     else {
  423.         theErr = kNilPtrErr;
  424.     }
  425.  
  426.     if (theErr != noErr) {
  427.         DebugPrint ("\pError in ASoundPause");
  428.     }
  429.  
  430.     return theErr;
  431. }
  432.  
  433. /*
  434.     Purpose:        If the sound is paused, resume playing.  If the sound is
  435.                     playing, pause playing.
  436.                     This differs from ASoundPause because it actually stops
  437.                     the sound instead of pausing it.  When the sound is
  438.                     paused for adjusting you can reset where the sound will
  439.                     next start playing from without having to play the
  440.                     remainder of the current buffer.  This routine is used
  441.                     for the QuickTime style playing.
  442.     Side Effects:    Removes the callback from the sound channel because
  443.                     otherwise while adjusting the sound the Sound Manager
  444.                     would call our clean up routine.
  445.                     When resuming a sound ASoundStartPlaying will install
  446.                     our callback routine if necessary (if the sound wasn't
  447.                     already paused).
  448. */
  449. /*-----------------------------------------------------------------------*/
  450. OSErr            ASoundPauseForAdjust    (SoundInfoPtr theSoundInfo)
  451. /*-----------------------------------------------------------------------*/
  452. {
  453.     SndCommand        theCmd            = {kInit, kInit, kInit};
  454.     OSErr            theErr            = noErr;
  455.     static Fixed    oldRate            = kInit;
  456.  
  457.     if (StrictIsValid (theSoundInfo)) {
  458.         if (theSoundInfo->adjusting == false) {
  459.             theCmd.cmd = flushCmd;            /* so the sound completion callback doesn't get called */
  460.             theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  461.             if (theErr == noErr) {
  462.                 theSoundInfo->adjusting = true;
  463.                 theCmd.cmd = quietCmd;
  464.                 theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  465.                 if (theErr == noErr) {
  466.                     theSoundInfo->hasBeenAdjusted = true;
  467.                     theSoundInfo->playing = false;
  468.                 }
  469.             }
  470.         }
  471.         else {
  472.             theSoundInfo->adjusting = false;
  473.             ASoundStartPlaying (theSoundInfo, nil);
  474.         }
  475.     }
  476.     else {
  477.         theErr = kNilPtrErr;
  478.     }
  479.  
  480.     if (theErr != noErr) {
  481.         DebugPrint ("\pError in ASoundPauseForAdjust");
  482.     }
  483.  
  484.     return theErr;
  485. }
  486.  
  487. /*
  488.     Purpose:        Sound is done playing, dispose of the memory we no
  489.                     longer need.
  490.     Side Effects:    None.
  491. */
  492. /*-----------------------------------------------------------------------*/
  493. OSErr            ASoundDonePlaying        (SoundInfoPtr theSoundInfo,
  494.                                         unsigned long options)
  495. /*-----------------------------------------------------------------------*/
  496. {
  497.     myParamBlockRec    *myPB            = nil;
  498.     OSErr            theErr            = noErr;
  499.     short             i                = kInit,
  500.                     savedVRefNum    = kInit,
  501.                     savedRefNum        = kInit;
  502.  
  503.     if (IsValid (theSoundInfo)) {
  504.         theSoundInfo->soundDone = false;            /* so we don't get called multiple times */
  505.         theSoundInfo->playing = false;
  506.  
  507.         savedVRefNum = theSoundInfo->vRefNum;
  508.         savedRefNum = theSoundInfo->refNum;
  509.         if ((options == kCloseFile) || (options > kCloseFile + kFreeMem)) {
  510.             DebugPrint ("\pInvalid selector passed to ASoundDonePlaying");
  511.             theErr = kBadValue;
  512.         }
  513.         else {
  514.             theSoundInfo->stopping = false;    /* Sound is done stopping, hopefully... */
  515.             if (options > kNoOptions) {
  516.                 myPB = (myParamBlockRec*)theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufOne]->dbUserInfo[kPBPtr];
  517.                 if (myPB != nil) {
  518.                     if (options == kCloseFile + kFreeMem) {
  519.                         theErr = FSClose (myPB->pb.ioParam.ioRefNum);
  520.                     }
  521.                     if (theErr == noErr) {
  522.                         for (i = kInit; i <= kOne; i++) {
  523.                             DisposeRoutineDescriptor (((myParamBlockRec*)theSoundInfo->doubleHeader.dbhBufferPtr[i]->dbUserInfo[kPBPtr])->pb.ioParam.ioCompletion);
  524.                             DisposePtr ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i]->dbUserInfo[kPBPtr]);
  525.                             /* Have to unhold memory that was held */
  526.                             UnholdMemory ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i], sizeof(SndDoubleBuffer) + ASoundGetBufferSize (theSoundInfo));
  527.                             DisposePtr ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i]);
  528.                         }
  529.                     }
  530.                     else {
  531.                         DebugPrint ("\pFSClose error!");
  532.                     }
  533.                     theErr = SndDisposeChannel (theSoundInfo->chan, true);
  534.                     DisposeRoutineDescriptor (ASoundGetSoundCallBack (theSoundInfo));
  535.                     DisposeRoutineDescriptor (theSoundInfo->doubleHeader.dbhDoubleBack);
  536.                     if (theErr != noErr) {
  537.                         DebugPrint ("\pSndDisposeChannel error!");
  538.                     }
  539.                 }
  540.             }
  541.  
  542.             if (options == kNoOptions) {
  543.                 ASoundSetBytesCopied (theSoundInfo, theSoundInfo->dataStart);
  544.                 ASoundSetCurBuffer (theSoundInfo, kStart);
  545.                 theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufOne]->dbFlags = nil;
  546.                 theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufTwo]->dbFlags = nil;
  547.                 theErr = ASoundPrimeBuffers (theSoundInfo);
  548.             }
  549.             if (options == kFreeMem) {
  550.                 ASoundInit (theSoundInfo);
  551.                 theSoundInfo->vRefNum = savedVRefNum;
  552.                 theSoundInfo->refNum = savedRefNum;
  553.             }
  554.             else {
  555.                 if (options == kCloseFile + kFreeMem) {
  556.                     ASoundInit (theSoundInfo);
  557.                 }
  558.             }
  559.         }
  560.     }
  561.     else {
  562.         theErr = kNilPtrErr;
  563.     }
  564.  
  565.     if (theErr != noErr) {
  566.         DebugPrint ("\pError in ASoundDonePlaying");
  567.     }
  568.  
  569.     return theErr;
  570. }
  571.  
  572. /*
  573.     Purpose:        Returns the channel for the sound in case you want to
  574.                     send it specific commands.
  575.     Side Effects:    None.
  576. */
  577. /*-----------------------------------------------------------------------*/
  578. SndChannelPtr    ASoundGetChan            (SoundInfoPtr theSoundInfo)
  579. /*-----------------------------------------------------------------------*/
  580. {
  581.     SndChannelPtr    returnValue;
  582.  
  583.     if (StrictIsValid (theSoundInfo)) {
  584.         returnValue = theSoundInfo->chan;
  585.     } else {
  586.         returnValue = nil;
  587.     }
  588.  
  589.     return returnValue;
  590. }
  591.  
  592. /*
  593.     Purpose:        Returns the name of the file containing the currently
  594.                     playing sound.
  595.     Side Effects:    None.
  596. */
  597. /*-----------------------------------------------------------------------*/
  598. OSErr            ASoundGetSoundName        (SoundInfoPtr theSoundInfo,
  599.                                         Str63 theName)
  600. /*-----------------------------------------------------------------------*/
  601. {
  602.     OSErr            theErr        = noErr;
  603.     short            i            = 0;
  604.  
  605.     if (StrictIsValid (theSoundInfo) && theName != nil) {
  606.         /* This should probably be a BlockMoveData */
  607.         for (i = 0; i <= theSoundInfo->theName[0]+1; i++)
  608.             theName[i] = theSoundInfo->theName[i];
  609.     }
  610.  
  611.     return theErr;
  612. }
  613.  
  614. /*
  615.     Purpose:        Gets the number of the current buffer
  616.                     (in the range 1 to ASoundGetNumBuffers()) of the
  617.                     currently playing sound.
  618.     Side Effects:    None.
  619. */
  620. /*-----------------------------------------------------------------------*/
  621. long            ASoundGetCurBuffer        (SoundInfoPtr theSoundInfo)
  622. /*-----------------------------------------------------------------------*/
  623. {
  624.     long        returnValue        = kInit;
  625.     OSErr        theErr            = noErr;
  626.  
  627.     if (StrictIsValid (theSoundInfo)) {
  628.         returnValue = theSoundInfo->currentBuffer;
  629.     }
  630.     else {
  631.         theErr = kNilPtrErr;
  632.         returnValue = nil;
  633.     }
  634.  
  635.     if (theErr != noErr) {
  636.         DebugPrint ("\pError in ASoundGetCurBuffer");
  637.     }
  638.  
  639.     return returnValue;
  640. }
  641.  
  642. /*
  643.     Purpose:        Sets which buffer should be the next buffer to play
  644.                     from (in the range 1 to ASoundGetNumBuffers())
  645.                     for the currently playing sound.
  646.     Side Effects:    None.
  647. */
  648. /*-----------------------------------------------------------------------*/
  649. OSErr            ASoundSetCurBuffer        (SoundInfoPtr theSoundInfo,
  650.                                         long newValue)
  651. /*-----------------------------------------------------------------------*/
  652. {
  653.     OSErr        theErr            = noErr;
  654.  
  655.     if (IsValid (theSoundInfo)) {
  656.         if ((newValue >= kStart) && (newValue <= ASoundGetNumBuffers (theSoundInfo))) {
  657.             theSoundInfo->currentBuffer = newValue;
  658.             ASoundSetBytesCopied (theSoundInfo, newValue * ASoundGetBufferSize (theSoundInfo) + theSoundInfo->dataStart);
  659.         }
  660.         else {
  661.             theErr = kBadRange;
  662.         }
  663.     }
  664.     else {
  665.         theErr = kNilPtrErr;
  666.     }
  667.  
  668.     return theErr;
  669. }
  670.  
  671. /*
  672.     Purpose:        Gets the number of buffers that the currently playing
  673.                     sound will need to play in its entirety.
  674.     Side Effects:    None.
  675. */
  676. /*-----------------------------------------------------------------------*/
  677. long            ASoundGetNumBuffers        (SoundInfoPtr theSoundInfo)
  678. /*-----------------------------------------------------------------------*/
  679. {
  680.     long        returnValue        = kInit;
  681.     OSErr        theErr            = noErr;
  682.  
  683.     if (IsValid (theSoundInfo)) {
  684.         returnValue = theSoundInfo->numBuffers;
  685.     }
  686.     else {
  687.         theErr = kNilPtrErr;
  688.         returnValue = nil;
  689.     }
  690.  
  691.     if (theErr != noErr) {
  692.         DebugPrint ("\pError in ASoundGetNumBuffers");
  693.     }
  694.  
  695.     return returnValue;
  696. }
  697.  
  698. /*
  699.     Purpose:        Gets the length (in bytes) of the currently playing
  700.                     sound.  This number does not include any header bytes.
  701.     Side Effects:    None.
  702. */
  703. /*-----------------------------------------------------------------------*/
  704. long            ASoundGetNumTotalBytes    (SoundInfoPtr theSoundInfo)
  705. /*-----------------------------------------------------------------------*/
  706. {
  707.     long        returnValue        = kInit;
  708.     OSErr        theErr            = noErr;
  709.  
  710.     if (IsValid (theSoundInfo)) {
  711.         returnValue = theSoundInfo->bytesTotal;
  712.     }
  713.     else {
  714.         theErr = kNilPtrErr;
  715.         returnValue = nil;
  716.     }
  717.  
  718.     if (theErr != noErr) {
  719.         DebugPrint ("\pError in ASoundGetNumTotalBytes");
  720.     }
  721.  
  722.     return returnValue;
  723. }
  724.  
  725. /*
  726.     Purpose:        Gets the number of bytes that will be played by the end
  727.                     of the current buffer of the currently playing sound.
  728.     Side Effects:    None.
  729. */
  730. /*-----------------------------------------------------------------------*/
  731. long            ASoundGetBytesCopied    (SoundInfoPtr theSoundInfo)
  732. /*-----------------------------------------------------------------------*/
  733. {
  734.     long        returnValue        = kInit;
  735.     OSErr        theErr            = noErr;
  736.  
  737.     if (IsValid (theSoundInfo)) {
  738.         returnValue = theSoundInfo->bytesCopied;
  739.     }
  740.     else {
  741.         theErr = kNilPtrErr;
  742.         returnValue = nil;
  743.     }
  744.  
  745.     if (theErr != noErr) {
  746.         DebugPrint ("\pError in ASoundGetBytesCopied");
  747.     }
  748.  
  749.     return returnValue;
  750. }
  751.  
  752. /*
  753.     Purpose:        Sets the location in the file where the next buffer
  754.                     should be filled from for the currently playing sound.
  755.     Side Effects:    None.
  756. */
  757. /*-----------------------------------------------------------------------*/
  758. OSErr            ASoundSetBytesCopied    (SoundInfoPtr theSoundInfo,
  759.                                         long newValue)
  760. /*-----------------------------------------------------------------------*/
  761. {
  762.     OSErr        theErr            = noErr;
  763.  
  764.     if (IsValid (theSoundInfo)) {
  765.         if (newValue >= theSoundInfo->dataStart || newValue == kInit) {
  766.             theSoundInfo->bytesCopied = newValue;
  767.         }
  768.         else {
  769.             theErr = kBadValue;
  770.         }
  771.     }
  772.     else {
  773.         theErr = kNilPtrErr;
  774.     }
  775.  
  776.     if (theErr != noErr) {
  777.         DebugPrint ("\pError in ASoundSetBytesCopied");
  778.     }
  779.  
  780.     return theErr;
  781. }
  782.  
  783. /*
  784.     Purpose:        Gets the size of a buffer of the currently playing
  785.                     sound.  Multiply by two to know how much memory is
  786.                     reserved for buffering the currently playing sound.
  787.     Side Effects:    None.
  788. */
  789. /*-----------------------------------------------------------------------*/
  790. long            ASoundGetBufferSize        (SoundInfoPtr theSoundInfo)
  791. /*-----------------------------------------------------------------------*/
  792. {
  793.     long        returnValue        = kInit;
  794.     OSErr        theErr            = noErr;
  795.  
  796.     if (IsValid (theSoundInfo)) {
  797.         returnValue = theSoundInfo->doubleBufferSize;
  798.     }
  799.     else {
  800.         theErr = kNilPtrErr;
  801.         returnValue = nil;
  802.     }
  803.  
  804.     if (theErr != noErr) {
  805.         DebugPrint ("\pError in ASoundGetBufferSize");
  806.     }
  807.  
  808.     return returnValue;
  809. }
  810.  
  811. /*
  812.     Purpose:        Gets the UPP for the function that should be called when
  813.                     the currently playing sound finishes.
  814.     Side Effects:    None.
  815. */
  816. /*-----------------------------------------------------------------------*/
  817. SndCallBackUPP    ASoundGetSoundCallBack    (SoundInfoPtr theSoundInfo)
  818. /*-----------------------------------------------------------------------*/
  819. {
  820.     SndCallBackUPP        returnValue        = nil;
  821.     OSErr                theErr            = noErr;
  822.  
  823.     if (IsValid (theSoundInfo)) {
  824.         returnValue = theSoundInfo->theSoundCallBackUPP;
  825.     }
  826.     else {
  827.         theErr = kNilPtrErr;
  828.         returnValue = nil;
  829.     }
  830.  
  831.     if (theErr != noErr) {
  832.         DebugPrint ("\pError in ASoundGetSoundCallBack");
  833.     }
  834.  
  835.     return returnValue;
  836. }
  837.  
  838. /*
  839.     Purpose:        Sets the function that should be called when the the
  840.                     currently playing sound finishes.
  841.     Side Effects:    None.
  842. */
  843. /*-----------------------------------------------------------------------*/
  844. OSErr            ASoundSetSoundCallBack    (SoundInfoPtr theSoundInfo,
  845.                                         void* newValue)
  846. /*-----------------------------------------------------------------------*/
  847. {
  848.     OSErr        theErr        = noErr;
  849.  
  850.     if (IsValid (theSoundInfo)) {
  851.         if (theSoundInfo->theSoundCallBackUPP != nil) {
  852.             DisposeRoutineDescriptor (theSoundInfo->theSoundCallBackUPP);
  853.         }
  854.         if (newValue != nil) {
  855.             theSoundInfo->theSoundCallBackUPP = NewSndCallBackProc(newValue);
  856.         }
  857.         else {
  858.             theSoundInfo->theSoundCallBackUPP = NewSndCallBackProc(ASoundDoneCallBack);
  859.             DebugPrint ("\pDid you really want the default sound callback?");
  860.         }
  861.     }
  862.     else {
  863.         theErr = kNilPtrErr;
  864.     }
  865.  
  866.     if (theErr != noErr) {
  867.         DebugPrint ("\pError in ASoundSetSoundCallBack");
  868.     }
  869.  
  870.     return theErr;
  871. }
  872.  
  873. /*
  874.     Purpose:        Says whether to play the currently playing sound backwards
  875.                     (it actually reverses the sound in the buffer).
  876.     Side Effects:    Takes effect when the next sound buffer gets filled.
  877. */
  878. /*-----------------------------------------------------------------------*/
  879. OSErr            ASoundPlayBackwards        (SoundInfoPtr theSoundInfo,
  880.                                         Boolean newValue)
  881. /*-----------------------------------------------------------------------*/
  882. {
  883.     OSErr        theErr        = noErr;
  884.  
  885.     if (StrictIsValid (theSoundInfo)) {
  886.         theSoundInfo->backwards = newValue;
  887.     }
  888.     else {
  889.         theErr = kNilPtrErr;
  890.     }
  891.  
  892.     if (theErr != noErr) {
  893.         DebugPrint ("\pError in ASoundPlayBackwards");
  894.     }
  895.  
  896.     return theErr;
  897. }
  898.  
  899. /*
  900.     Purpose:        Returns true if the currently playing sound's buffer
  901.                     is set to be reversed.
  902.     Side Effects:    None.
  903. */
  904. /*-----------------------------------------------------------------------*/
  905. Boolean            ASoundIsBackwards        (SoundInfoPtr theSoundInfo)
  906. /*-----------------------------------------------------------------------*/
  907. {
  908.     OSErr        theErr            = noErr;
  909.     Boolean        returnValue        = false;
  910.  
  911.     if (StrictIsValid (theSoundInfo)) {
  912.         returnValue = theSoundInfo->backwards;
  913.     }
  914.     else {
  915.         theErr = kNilPtrErr;
  916.     }
  917.  
  918.     if (theErr != noErr) {
  919.         DebugPrint ("\pError in ASoundIsBackwards");
  920.     }
  921.  
  922.     return returnValue;
  923. }
  924.  
  925. /*
  926.     Purpose:        Returns true if the sound has finished playing.
  927.     Side Effects:    None.
  928. */
  929. /*-----------------------------------------------------------------------*/
  930. Boolean            ASoundIsDone            (SoundInfoPtr theSoundInfo)
  931. /*-----------------------------------------------------------------------*/
  932. {
  933.     OSErr        theErr            = noErr;
  934.     Boolean        returnValue        = false;
  935.  
  936.     if (StrictIsValid (theSoundInfo)) {
  937.         returnValue = theSoundInfo->soundDone;
  938.     }
  939.     else {
  940.         theErr = kNilPtrErr;
  941.     }
  942.  
  943.     if (theErr != noErr) {
  944.         DebugPrint ("\pError in ASoundIsDone");
  945.     }
  946.  
  947.     return returnValue;
  948. }
  949.  
  950. /*
  951.     Purpose:        Changes the volume of the currently playing sound.
  952.                     The values you pass in are added to the current values.
  953.                     Negitive values will decrease the volume, positive values
  954.                     will increase the volume.
  955.     Side Effects:    None.
  956. */
  957. /*-----------------------------------------------------------------------*/
  958. OSErr            ASoundChangeVolume        (SoundInfoPtr theSoundInfo,
  959.                                         unsigned short leftVol,
  960.                                         unsigned short rightVol)
  961. /*-----------------------------------------------------------------------*/
  962. {
  963.     SndCommand        theCmd        = {volumeCmd, kInit, kInit};
  964.     OSErr            theErr        = noErr;
  965.     unsigned short    tempLeft    = kInit,
  966.                     tempRight    = kInit;
  967.  
  968.     if (StrictIsValid (theSoundInfo)) {
  969.         theErr = ASoundGetVolume (theSoundInfo, &tempLeft, &tempRight);
  970.         if (theErr == noErr) {
  971.             if ((tempLeft + leftVol) > kMaxVolume) leftVol = kMaxVolume - tempLeft;
  972.             if ((tempRight + rightVol) > kMaxVolume) rightVol = kMaxVolume - tempRight;
  973.             if ((tempLeft + leftVol) < kMinVolume) leftVol = -tempLeft;
  974.             if ((tempRight + rightVol) < kMinVolume) rightVol = -tempRight;
  975.             theErr = ASoundSetVolume (theSoundInfo, tempLeft+leftVol, tempRight+rightVol);
  976.         }
  977.     }
  978.     else {
  979.         theErr = kNilPtrErr;
  980.     }
  981.  
  982.     if (theErr != noErr) {
  983.         DebugPrint ("\pError in ASoundChangeVolume");
  984.     }
  985.  
  986.     return theErr;
  987. }
  988.  
  989. /*
  990.     Purpose:        Gets the volume of the currently playing sound.
  991.     Side Effects:    None.
  992. */
  993. /*-----------------------------------------------------------------------*/
  994. OSErr            ASoundGetVolume            (SoundInfoPtr theSoundInfo,
  995.                                         unsigned short *leftVol,
  996.                                         unsigned short *rightVol)
  997. /*-----------------------------------------------------------------------*/
  998. {
  999.     SndCommand        theCmd        = {getVolumeCmd, kInit, kInit};
  1000.     unsigned long    theVol        = kInit;
  1001.     OSErr            theErr        = noErr;
  1002.  
  1003.     if (StrictIsValid (theSoundInfo)) {
  1004.         theCmd.param2 = (long)&theVol;
  1005.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1006.  
  1007.         if (theErr == noErr) {
  1008.             *leftVol = theVol & kLeftMask;
  1009.             *rightVol = theVol >> kSixteen;
  1010.         }
  1011.     }
  1012.     else {
  1013.         theErr = kNilPtrErr;
  1014.     }
  1015.  
  1016.     if (theErr != noErr) {
  1017.         DebugPrint ("\pError in ASoundGetVolume");
  1018.     }
  1019.  
  1020.     return theErr;
  1021. }
  1022.  
  1023. /*
  1024.     Purpose:        Sets the volume of the currently playing sound.
  1025.     Side Effects:    None.
  1026. */
  1027. /*-----------------------------------------------------------------------*/
  1028. OSErr            ASoundSetVolume            (SoundInfoPtr theSoundInfo,
  1029.                                         unsigned short leftVol,
  1030.                                         unsigned short rightVol)
  1031. /*-----------------------------------------------------------------------*/
  1032. {
  1033.     SndCommand        theCmd        = {volumeCmd, kInit, kInit};
  1034.     OSErr            theErr        = noErr;
  1035.  
  1036.     if (StrictIsValid (theSoundInfo)) {
  1037.         theCmd.param2 = (rightVol << kSixteen) | leftVol;
  1038.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1039.     }
  1040.     else {
  1041.         theErr = kNilPtrErr;
  1042.     }
  1043.  
  1044.     if (theErr != noErr) {
  1045.         DebugPrint ("\pError in ASoundSetVolume");
  1046.     }
  1047.  
  1048.     return theErr;
  1049. }
  1050.  
  1051. /*
  1052.     Purpose:        Gets the rate multiplier of the currently playing sound.
  1053.     Side Effects:    None.
  1054. */
  1055. /*-----------------------------------------------------------------------*/
  1056. OSErr            ASoundGetRateMul        (SoundInfoPtr theSoundInfo,
  1057.                                         UnsignedFixed *theRateMul)
  1058. /*-----------------------------------------------------------------------*/
  1059. {
  1060.     SndCommand        theCmd        = {getRateMultiplierCmd, kInit, kInit};
  1061.     OSErr            theErr        = noErr;
  1062.  
  1063.     if (StrictIsValid (theSoundInfo)) {
  1064.         theCmd.param2 = (long)theRateMul;
  1065.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1066.     }
  1067.     else {
  1068.         theErr = kNilPtrErr;
  1069.     }
  1070.  
  1071.     if (theErr != noErr) {
  1072.         DebugPrint ("\pError in ASoundGetRateMul");
  1073.     }
  1074.  
  1075.     return theErr;
  1076. }
  1077.  
  1078. /*
  1079.     Purpose:        Gets the rate multiplier of the currently playing sound.
  1080.     Side Effects:    None.
  1081. */
  1082. /*-----------------------------------------------------------------------*/
  1083. OSErr            ASoundSetRateMul        (SoundInfoPtr theSoundInfo,
  1084.                                         UnsignedFixed theRateMul)
  1085. /*-----------------------------------------------------------------------*/
  1086. {
  1087.     SndCommand        theCmd        = {rateMultiplierCmd, kInit, kInit};
  1088.     OSErr            theErr        = noErr;
  1089.  
  1090.     if (StrictIsValid (theSoundInfo)) {
  1091.         theCmd.param2 = (long)theRateMul;
  1092.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1093.     }
  1094.     else {
  1095.         theErr = kNilPtrErr;
  1096.     }
  1097.  
  1098.     if (theErr != noErr) {
  1099.         DebugPrint ("\pError in ASoundSetRateMul");
  1100.     }
  1101.  
  1102.     return theErr;
  1103. }
  1104.